package com.iflytek.aisol.audit.iflytek.strategy.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.iflytek.aisol.audit.iflytek.strategy.manage.config.CustomException;
import com.iflytek.aisol.audit.iflytek.strategy.manage.constant.TableColumnInitConstant;
import com.iflytek.aisol.audit.iflytek.strategy.manage.dto.EngineParamDTO;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.EngineParam;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.EngineParamMapper;
import com.iflytek.aisol.audit.iflytek.strategy.manage.service.EngineParamService;
import com.iflytek.aisol.audit.iflytek.strategy.manage.utils.ConfigValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
// 引擎参数表新增修改之后 增加判断防重逻辑
@Service
public class EngineParamServiceImpl implements EngineParamService {
    @Autowired
    private EngineParamMapper engineParamMapper;

    /**
     * 新增引擎参数配置接口
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(EngineParamDTO engineParamDTO) {
        // 获取要新增的字段名称
        String paramName = engineParamDTO.getParamName();
        // 获取引擎类型
        String engineType = engineParamDTO.getEngineType();
        // 根据引擎类型判断是否是channel或者server的枚举类型
        if(!(ConfigValidationUtil.isInServerType(engineType) ||
                ConfigValidationUtil.isInChannelType(engineType))){
            throw new CustomException(TableColumnInitConstant.ERROR, "输入的引擎类型有误");
        }
        // 新增操作之前，增加判断防重逻辑
        EngineParam ep = engineParamMapper.findByEngineTypeAndParamName(engineType,paramName);
        // 如果记录存在，抛出异常
        if (ep != null) {
            throw new CustomException("engineType=" + engineType + ", paramName=" + paramName + " 的记录已存在");
        }
        // 校验通过
        EngineParam engineParam = BeanUtil.toBean(engineParamDTO, EngineParam.class);
        engineParamMapper.save(engineParam);
    }

    /**
     * 修改引擎参数配置接口
     * @param id 根据id修改
     * @param engineParamDTO 前端传入的
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(Long id, EngineParamDTO engineParamDTO) {
        EngineParam original = engineParamMapper.getOne(id);
        original.setEngineType(engineParamDTO.getEngineType());
        original.setParamType(engineParamDTO.getParamType());
        original.setParamName(engineParamDTO.getParamName());
        original.setInputType(engineParamDTO.getInputType());
        // 更新操作之前，增加判断防重逻辑
        EngineParam ep = engineParamMapper.findByEngineTypeAndParamName(original.getEngineType(),original.getParamName());
        // 如果记录存在，抛出异常
        if (ep != null) {
            throw new CustomException("engineType=" + original.getEngineType() + ", paramName=" + original.getParamName() + " 的记录已存在");
        }
        engineParamMapper.save(original);
    }
    /**
     * 删除引擎参数配置接口
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Long id) {
        engineParamMapper.deleteById(id);
    }

    @Override
    public EngineParamDTO query(Long id) {
        EngineParam engineParam = engineParamMapper.getOne(id);
        return BeanUtil.toBean(engineParam, EngineParamDTO.class);
    }


}
